﻿using System.Collections;
using System.Collections.Generic;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.UI.Buttons;
using Assets.Scripts.UI.Panel;
using UnityEngine;
using UnityEngine.UI;
using Button = UnityEngine.UI.Button;
using Assets.Scripts.Tool;

namespace Assets.Scripts.UI.Item
{
    public class CourseItem:UIBase<CourseItem>
    {
        public Text CourseNameText;
        public Button CourseButton;

        public int CourseId { get; set; }

        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
        }

        public void Init(CourseInfo courseInfo, string courseType, Transform[] parents,CheckType type=CheckType.Null)
        {
            CourseNameText.text = courseInfo.Name;
            CourseId = courseInfo.Id;
            switch (courseType)
            {
                case NetConstValue.Collect_CourseInfo://收藏夹
                    CourseButton.onClick.AddListener(() => StartCoroutine(OnCollectButtonClick(courseInfo.Id, parents[0])));
                    break;
                case NetConstValue.HW_CourseInfo://作业
                    CourseButton.onClick.AddListener(()=>OnHWButtonClick(courseInfo,parents[0],parents[1]));
                    break;
                case NetConstValue.CS_CourseInfo://查询分数的信息
                    CourseButton.onClick.AddListener(()=>OnCSButtonClick(CourseId,parents[0],type));
                    break;
                case NetConstValue.Mock_CourseInfo://模拟考
                    CourseButton.onClick.AddListener(()=>OnMockButtonClick(courseInfo.Id,parents[0]));
                    break;
            }
        }

        /// <summary>
        /// 收藏夹
        /// </summary>
        private IEnumerator OnCollectButtonClick(int courseId, Transform parent)
        {
            if (CollectPanel.NowClickItem == this)
            {
                yield break;
            }
            else
            {
                if (CollectPanel.NowClickItem != null)
                {
                    CollectPanel.NowClickItem.SetNormalState();
                }
                CollectPanel.NowClickItem = this;
            }
            //取得相应的数据
            CollectPanel._Instance.OnCourseButtonClick(courseId);
            SetPressedState();
        }

        /// <summary>
        /// 作业
        /// </summary>
        private void OnHWButtonClick(CourseInfo info, Transform finish, Transform unfinish)
        {
            if (HomeWorkPanel.NowClickCourseItem == this)
            {
                return;
            }
            else
            {
                if (HomeWorkPanel.NowClickCourseItem != null)
                {
                    HomeWorkPanel.NowClickCourseItem.SetNormalState();
                }               
                HomeWorkPanel.NowClickCourseItem = this;
            }
            CleanTransformChildren(finish);
            CleanTransformChildren(unfinish);
            List<HomeWorkInfo> hwInfos =
                NetReaderFactory.Instance().GetInfoByName(NetConstValue.HW_HomeInfo) as List<HomeWorkInfo>;
            //Debug.Log("homeworkinfos.count:" + hwInfos.Count);
            if (hwInfos == null) return;
            int unFinishNum = 0, finishNum = 0;
            for (int i = 0; i < hwInfos.Count; i++)
            {
                if (hwInfos[i].CourseId == info.Id)
                {
                    if (hwInfos[i].State == 1)//完成
                    {
                        finishNum++;
                        Transform tsf = CreateItem(ItemType.HWFinishItem);
                        tsf.SetParent(finish);
                        tsf.localPosition = Vector3.zero;
                        tsf.localScale = Vector3.one;
                        HWFinishItem item = tsf.GetComponent<HWFinishItem>();
                        item.Init(hwInfos[i], finishNum);
                    }
                    else//未完成
                    {
                        unFinishNum++;
                        Transform tsf = CreateItem(ItemType.HWUnFinishItem);
                        tsf.SetParent(unfinish);
                        tsf.localPosition = Vector3.zero;
                        tsf.localScale = Vector3.one;
                        HWUnFinishItem item = tsf.GetComponent<HWUnFinishItem>();
                        item.Init(hwInfos[i], unFinishNum);
                    }
                }
            }
            SetPressedState();
        }

        /// <summary>
        /// 查询成绩
        /// </summary>
        /// <param name="type"></param>
        /// <param name="courseid"></param>
        /// <param name="parent"></param>
        private void OnCSButtonClick(int courseid,Transform parent,CheckType type)
        {
            if (CheckScorePanel.NowClickCourseItem == this)
            {
                return;
            }
            CleanTransformChildren(CheckScorePanel._Instance.Content_Item);
            List<PaperInfo> paperInfos =
            NetReaderFactory.Instance().GetInfoByType<List<PaperInfo>>() as List<PaperInfo>;
            if(paperInfos==null)return;
            int paperNum = 0;
            for (int i = 0; i < paperInfos.Count; i++)
            {
                if (paperInfos[i].CourseId == courseid)
                {
                    Transform tsf = CreateItem(ItemType.PaperItem);
                    tsf.SetParent(parent);
                    tsf.localScale=Vector3.one;
                    tsf.localPosition=Vector3.zero;  
                    paperNum++;
                    PaperItem item = tsf.GetComponent<PaperItem>();
                    item.Init(paperInfos[i],paperNum,false,type);
                }
            }
            if (CheckScorePanel.NowClickCourseItem != null)
            {
                CheckScorePanel.NowClickCourseItem.SetNormalState();
            }
            CheckScorePanel.NowClickCourseItem = this;
            SetPressedState();
        }

        /// <summary>
        /// 模拟考
        /// </summary>
        private void OnMockButtonClick(int courseId,Transform parent)
        {
            if (MockExamPanel._Instance.NowClickItem == this) return;
            CleanTransformChildren(parent);//清空
            //取得相应的数据
            List<PaperInfo> paperInfos =
                NetReaderFactory.Instance().GetInfoByName(NetConstValue.Mock_PaperInfo) as List<PaperInfo>;
            //Debug.Log("paperinfos.count:"+paperInfos.Count);
            if (paperInfos == null) return;
            int paperNum = 0;
            for (int i = 0; i < paperInfos.Count; i++)
            {
                if (paperInfos[i].CourseId == courseId)
                {
                    Transform tsf = CreateItem(ItemType.PaperItem);
                    tsf.SetParent(parent);
                    tsf.localScale = Vector3.one;
                    tsf.localPosition = Vector3.zero;
                    paperNum++;
                    PaperItem item = tsf.GetComponent<PaperItem>();
                    item.Init(paperInfos[i], paperNum,true);
                }
            }
            if (MockExamPanel._Instance.NowClickItem != null)
            {
                MockExamPanel._Instance.NowClickItem.SetNormalState();               
            }
            MockExamPanel._Instance.NowClickItem = this;
            SetPressedState();
        }

        public void SetNormalState()
        {
            CourseButton.GetComponent<ButtonSprite>().SetNormalSprite();
            CourseButton.GetComponent<ButtonColor>().SetNormalColor();
        }

        public void SetPressedState()
        {
            CourseButton.GetComponent<ButtonSprite>().SetPressedSprite();
            CourseButton.GetComponent<ButtonColor>().SetPressedColor();
        }
    }
}